﻿# coding:utf-8
from django.shortcuts import redirect
from django.shortcuts import render
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib import auth
from django.views.decorators.csrf import csrf_exempt


def index(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None and user.is_active:
            login(request, user)
            return redirect('home')
        else:
            messages.error(request, '''用户名或者密码不正确，请重新输入 !''')
    return render(request, "web/login.html", {})


@login_required
def home(request):
    if request.user.user.type == 0:
        return render(request, "student/home.html", {})
    if request.user.user.type == 2:
        return render(request, "teacher/homedef.html", {})


@login_required
def logout(request):
    auth.logout(request)
    return redirect('index')


@csrf_exempt
def sendemail(request):
    if request.method == "POST":
        title= request.POST["title"]
        toemail= request.POST["toemail"]
        subtoemail= request.POST["subemail"]
        neirong= request.POST["neirong"]
        # toemail=request.POST[" toemail"]
        # subtoemail=request.POST["subtoemail"]
        # title=request.POST["title"]
    return redirect('teacher/def')
